import {gethomecasual,getcategory,gethomeshoplist,getuserinfo,getlogout} from "@/api/index.js"

 const state={
   
    homecasual:[],
    categoryList:[],
    shoplist:[],
    userinfo:{}
  }
 const  mutations={
    GETHOME(state,homecasual){
      state.homecasual=homecasual
  
    },
    GETCATE(state,categoryList){
      state.categoryList=categoryList
    },
    GETLIST(state,shoplist){
      state.shoplist=shoplist
    },
    SYNCUSERINFO(state,{userinfo}){
      state.userinfo=userinfo
    },
    result_userinfo(state){
      state.userinfo={}
    }
   

  }
  const actions={
    //首页轮播图
   async gethome({commit}){
  let result=await gethomecasual()

  commit("GETHOME",result.data.data.msg)
    },
    //首页轮播图旁边数组
    async getcate({commit}){
      let res =await getcategory()
     
      commit("GETCATE",res.data.msg)
    },
    async getlist({commit}){
      let resu =await gethomeshoplist()
     commit("GETLIST",resu.data.message)

    },
    //登录保持userinfox信息
     syncUserInfo({commit},userinfo){
      commit("SYNCUSERINFO",{userinfo})
    },
    // 存储首页用户信息
    async getuserinfo({commit},params){
      let userinfo={};
      const  result= await getuserinfo(params)
      if(result.data.code==200){
        userinfo=result.data.msg
        console.log(result.data.msg);
        window.localStorage.setItem('userinfo',JSON.stringify(result.data.msg))

      }else{
        userinfo=JSON.parse(window.localStorage.getItem('userinfo'))
        console.log(111);
        console.log(userinfo);
      }
      commit("SYNCUSERINFO",{userinfo})
    },
    
    async logout({commit}){
      let result=await getlogout();
      if(result.data.code==200){
        commit("result_userinfo")
      }

    },

    
   

    
      
  
    }
    const getters={
      categoryList(state){
        return state.categoryList
      },
      homecasual(state){
        return state. homecasual
      },
      shoplist(state){
        return state.shoplist
      },
      userinfo(state){
return state.userinfo
      }
      
    }


 

export default{
  state,
  actions,
  mutations,
  getters,
}
 